package com.hilinos.ftp;

import com.alibaba.fastjson.JSON;
import com.hilinos.common.util.RandomUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

@Slf4j
public class UIApplication extends javax.swing.JFrame {

    private static final Dimension DIMENSION = Toolkit.getDefaultToolkit().getScreenSize();

    private static final int WIDTH = 800, HEIGHT = 480;
    private static final JFrame J_FRAME = new JFrame();
    private static final Vector<String> USERS = new Vector<>();
    private static final JList<String> USER_LIST = new JList<>();
    private static final boolean isStart = true;

    public static void start() {
        J_FRAME.setSize(WIDTH, HEIGHT);
        J_FRAME.setTitle("ftp用户管理器");
        J_FRAME.setLocation((DIMENSION.width - WIDTH) / 2, (DIMENSION.height - HEIGHT) / 2);
        J_FRAME.setLayout(new BorderLayout());

        USER_LIST.addListSelectionListener(e -> {
            if (e.getValueIsAdjusting()) {
                if (isStart) {
                    JOptionPane.showMessageDialog(J_FRAME, "服务正在启动，请先等服务启动完成", null, JOptionPane.INFORMATION_MESSAGE);
                }
                log.info("点击了: {}", USER_LIST.getSelectedValue());
            }
        });

        JScrollPane jScrollPane = new JScrollPane(USER_LIST);
        jScrollPane.setPreferredSize(new Dimension(260, HEIGHT));

        J_FRAME.add(jScrollPane, BorderLayout.WEST);

        jShow();
    }

    public static void jShow() {
        J_FRAME.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        J_FRAME.setVisible(true);
    }

    public static void addUser(String user) {
        USERS.add(user);
        USER_LIST.setListData(USERS);
    }

}
